package cn.yunhe.controller;

import cn.yunhe.pojo.Product;
import cn.yunhe.pojo.ResultRequest;
import cn.yunhe.pojo.ResultResponse;
import cn.yunhe.service.IProductService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    IProductService productService;

    /**
     * 前台需要获取到查询的数据列表（集合），同时还需要有分页相关的信息
     * @param
     * @param
     * @return
     */
   /*@RequestMapping("/queryAll")
    public ResultResponse queryAllProduct(@RequestBody(required = false) Product product,
                                          @RequestParam(defaultValue = "1",value = "page") Integer pageIndex,
                                          @RequestParam(defaultValue = "3") Integer pageSize){
        List<Product> productList = productService.queryAll(product,pageIndex,pageSize);
        PageInfo<Product> pageInfo = new PageInfo<Product>(productList);

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(productList);
        //此处不需要在返回分页信息，只需要给一个总条数即可，layui会自动分页
        resultResponse.setCount(pageInfo.getTotal());
        return resultResponse;
    }*/

    @RequestMapping("/queryAll")
    public ResultResponse queryAllProduct(@RequestBody ResultRequest<Product> resultRequest){
        List<Product> productList = productService.queryAll(resultRequest.getData(),resultRequest.getPage(),resultRequest.getLimit());
        PageInfo<Product> pageInfo = new PageInfo<Product>(productList);

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(productList);
        //此处不需要在返回分页信息，只需要给一个总条数即可，layui会自动分页
        resultResponse.setCount(pageInfo.getTotal());
        return resultResponse;
    }

   /* @RequestMapping("/queryAllTest")
    public ResultResponse queryAllProductTest(@RequestBody(required = false) Map<String,Product> requestMap,
                                          @RequestParam(defaultValue = "1",value = "page") Integer page,
                                          @RequestParam(defaultValue = "3") Integer limit){
        List<Product> productList = productService.queryAll(requestMap.get("product"),page,limit);
        PageInfo<Product> pageInfo = new PageInfo<Product>(productList);

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(productList);
        //此处不需要在返回分页信息，只需要给一个总条数即可，layui会自动分页
        resultResponse.setCount(pageInfo.getTotal());
        return resultResponse;
    }*/

}
